package com.school.lab.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.school.lab.common.dao.impl.GenericDaoHibernateImpl;
import com.school.lab.dao.UserDao;
import com.school.lab.domain.User;


public class UserDaoImpl extends GenericDaoHibernateImpl<User, Long> implements
		UserDao {


	@SuppressWarnings("unchecked")
	public List<User> findByType(int start, int limit, int flag, String userType) {
		StringBuffer hql = new StringBuffer();
		if ("admin".equals(userType)) {
			hql.append("from User a  where a.role.admin=:userType");
		} else {
			hql.append("from User a  where a.role.name=:userType");
		}
		hql.append(" and (a.famPassword is null or a.famPassword ='0') order by a.createDate desc,a.id");
		Query query = getSession().createQuery(hql.toString());
		query.setString("userType", userType);
		if (flag == 1) {
			query.setFirstResult(start);
			query.setMaxResults(limit);
		}

		return (List<User>)query.list();
	}

	public User getUser(String username, String password, String city,
			String flag) {
		StringBuffer hql = new StringBuffer();
		hql
				.append("from User u  where u.username=:username and u.password=:password and u.city=:city");
		if ("1".equals(flag)) {
			hql.append(" and u.role.admin=admin");
		} else {
			hql.append(" and u.role.admin=1");
		}
		Query query = getSession().createQuery(hql.toString());
		query.setString("username", username);
		query.setString("password", password);
		query.setString("city", city);
		return (User) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<User> findByType(int start, int limit, int flag,
			String userType, String city,int isTopAdmin) {
		StringBuffer hql = new StringBuffer();
		if ("admin".equals(userType)) {
			hql
					.append("from User a  where a.role.admin=:userType");
		} else {
			hql
					.append("from User a  where a.role.name=:userType");
		}
		
		if(isTopAdmin!=1){
			hql.append(" and a.city=:city");
		}

		hql.append(" order by a.createDate desc,a.id");

		Query query = getSession().createQuery(hql.toString());
		query.setString("userType", userType);
		if(isTopAdmin!=1){
		  query.setString("city", city);
		}
		if (flag == 1) {
			query.setFirstResult(start);
			query.setMaxResults(limit);
		}

		return (List<User>)query.list();
	}

	public List<User> findUser(int start, int limit, int flag, String roleName,
			String city, String realName) {
		StringBuffer hql = new StringBuffer();

		if ("admin".equals(roleName)) {
			hql.append("from User u  where u.role.admin=:roleName ");
		} else {
			hql.append("from User u  where u.role.name=:roleName ");
		}
		if (city != null) {
			hql.append(" and  u.city=:city");
		}
		if (realName != null) {
			hql.append(" and  u.realName=:realName");
		}
		hql.append(" order by u.createDate desc,u.id");
		
		Query query = getSession().createQuery(hql.toString());
		query.setString("roleName", roleName);
		if (city != null) {
			query.setString("city", city);
		}
		if (realName != null) {
			query.setString("realName", realName);
		}
		if (flag == 1) {
			query.setFirstResult(start);
			query.setMaxResults(limit);
		}

		return query.list();
	}

	public User getUser(String username, String password) {
		StringBuffer hql = new StringBuffer();
		hql
				.append("from User u  where u.username=:username and u.password=:password");

		Query query = getSession().createQuery(hql.toString());
		query.setString("username", username);
		query.setString("password", password);
		return (User) query.uniqueResult();
	}


	public User searchUser(String username, String realName) {

		StringBuffer hql = new StringBuffer();
		hql.append("from User u  where 1=1");
		if (username != null && !"".equals(username)) {
			hql.append(" and u.username=:username");
		}
		if (realName != null && !"".equals(realName)) {
			hql.append(" and u.realName=:realName");
		}
		Query query = getSession().createQuery(hql.toString());
		if (username != null && !"".equals(username)) {
			query.setString("username", username);
		}
		if (realName != null && !"".equals(realName)) {
			query.setString("realName", realName);
		}

		return (User) query.uniqueResult();
	}

	public User getByName(String username) {
		StringBuffer hql = new StringBuffer();
		hql.append("from User u where u.username=:username ");
		Query query = getSession().createQuery(hql.toString());
		query.setParameter("username", username);
		
		return (User)query.uniqueResult();
	}

	public List<User> findUser(int start, int limit, int flag, String roleName,
			String city, String realName, String username, String grade) {
		StringBuffer hql = new StringBuffer();

		if ("admin".equals(roleName)) {
			hql.append("from User u  where u.role.admin=:roleName ");
		} else {
			hql.append("from User u  where u.role.name=:roleName ");
		}
		if (city != null) {
			hql.append(" and  u.city=:city");
		}
		if (realName != null) {
			hql.append(" and  u.realName=:realName");
		}
		if(username!=null){
			hql.append(" and u.username=:username");
		}
		if(grade!=null){
			hql.append(" and u.grade=:grade");
		}
		hql.append(" order by u.createDate desc,u.id");
		
		Query query = getSession().createQuery(hql.toString());
		query.setString("roleName", roleName);
		if (city != null) {
			query.setString("city", city);
		}
		if (realName != null) {
			query.setString("realName", realName);
		}
		if(username!=null){
			query.setString("username", username);
		}
		if(grade!=null){
			query.setString("grade", grade);
		}
		if (flag == 1) {
			query.setFirstResult(start);
			query.setMaxResults(limit);
		}

		return query.list();
	}

}
